﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;

namespace ShareMarking.DiyQrCode
{
    public partial class a : System.Web.UI.Page
    {
        #region 实时数据属性
        protected string Province { get; set; }//来源地域(省)
        protected string City { get; set; }//来源地域(市)
        protected string IP { get; set; }//客户端ip地址
        protected string ISP { get; set; }//网络供应商
        #endregion

        #region 初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetIpInfo();
            }
        }
        #endregion

        #region 获取IP地址/省份/服务商/
        public void GetIpInfo()
        {
            try
            {
                string IpAddress = GetClientIp();

                if (IpAddress != "")
                {
                    string tbJson = "http://ip.taobao.com/service/getIpInfo.php?ip=" + IpAddress;
                    Uri tbUri = new Uri(tbJson);
                    WebRequest tbWebreq = WebRequest.Create(tbUri);
                    Stream tbStream = tbWebreq.GetResponse().GetResponseStream();
                    StreamReader tbsr = new StreamReader(tbStream, Encoding.Default);
                    string tbAll = tbsr.ReadToEnd(); //读取网站返回的数据
                    JObject jo = (JObject)JsonConvert.DeserializeObject(tbAll);//解析Json数据
                    IP = IpAddress.ToString();
                    ISP = jo["data"]["isp"].ToString();
                    Province = jo["data"]["region"].ToString();
                    City = jo["data"]["city"].ToString();
                }
                else
                {
                    IP = "未知IP";
                    ISP = "未知";
                    Province = "未知";
                    City = "未知";
                }
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("获取IP地址/省份/服务商/错误！" + ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 获取客户端IP地址

        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns></returns>
        public String GetClientIp()
        {
            String clientIP = "";
            if (System.Web.HttpContext.Current != null)
            {
                clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))
                {
                    clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
                    if (string.IsNullOrEmpty(clientIP))
                    {
                        clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                }
                else
                {
                    clientIP = clientIP.Split(',')[0];
                }
            }
            return clientIP;
        }

        #endregion
    }
}